//
//  YWUnlockView.m
//  YWUnlock
//
//  Created by Bruce on 2017/2/24.
//  Copyright © 2017年 dyw. All rights reserved.
//

#import "YWUnlockView.h"
#import "YWUnlockMacros.h"
#import "YWUnlockPreviewView.h"
#import "YWGesturesUnlockView.h"
#import "MYSystemAlertView.h"


@interface YWUnlockView ()
/** 头像 */
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
/** 绘制密码的状态label */
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
/** 重新绘制按钮 */
@property (weak, nonatomic) IBOutlet UIButton *resetGesturesPasswordButton;
/** 手势密码预览图 */
@property (weak, nonatomic) IBOutlet YWUnlockPreviewView *unlockPreviewView;
/** 手势密码绘制视图 */
@property (weak, nonatomic) IBOutlet YWGesturesUnlockView *gesturesUnlockView;
/** 当前创建的手势密码 */
@property (nonatomic, copy) NSString *curentGesturePassword;
/** 当前处理密码类型 (默认是创建密码) */
@property (nonatomic, assign) YWUnlockViewType type;
/** 操作结果 回调 */
@property (nonatomic, copy) CallBackBlock block;

@end

@implementation YWUnlockView
+ (instancetype)shareUnlockView
{
    static YWUnlockView *singleton;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton = [YWUnlock_BD loadNibNamed:@"YWUnlockView" owner:nil options:nil].lastObject;
        [singleton addBottomButton];
    });
    return singleton;
}

- (void)addBottomButton
{
    UIView *view = [[UIView alloc] init];
    [self addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.bottom.mas_equalTo(-(MY_BottomSafeMargin + MY_SpacingAndMargin_Spa5));
        make.left.mas_equalTo(0);
    }];
    
    //center
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = MY_Text_Color_Theme1;
    [view addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(view);
        make.size.mas_equalTo(CGSizeMake(2, 18));
    }];
    
    //忘记手势密码
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setTitle:@"忘记手势密码" forState:UIControlStateNormal];
    button1.titleLabel.font = [UIFont systemFontOfSize:16];
    [button1 setTitleColor:MY_Text_Color_Theme1 forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(forgetPassword) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button1];
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(line.mas_left).offset(-MY_SpacingAndMargin_Spa5);
        make.centerY.equalTo(view);
    }];
    
    //使用其他账户登录
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setTitle:@"使用其他账户登录" forState:UIControlStateNormal];
    button2.titleLabel.font = [UIFont systemFontOfSize:16];
    [button2 setTitleColor:MY_Text_Color_Theme1 forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(useOtherLogin) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button2];
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_right).offset(MY_SpacingAndMargin_Spa5);
        make.centerY.equalTo(view);
    }];
}

/// 忘记手势密码
- (void)forgetPassword
{
    [JQLiveTools logout];
    [JQLiveTools deleteGesturePassword];
}

/// 使用其他账号登录
- (void)useOtherLogin
{
    [JQLiveTools logout];

}
#pragma mark - life cycle
- (void)awakeFromNib{
    [super awakeFromNib];
    self.type = YWUnlockViewCreate;//默认是创建密码
}

#pragma mark - private methods
/** 根据不同的类型处理 */
- (void)handleWithType:(YWUnlockViewType)type password:(NSString *)gesturePassword{
    switch (type) {
        case YWUnlockViewCreate://创建手势密码
            [self createGesturesPassword:gesturePassword];
            break;
        case YWUnlockViewUnlock://解锁手势密码
            [self validateGesturesPassword:gesturePassword];
            break;
    }
}

//创建手势密码
- (void)createGesturesPassword:(NSString *)gesturesPassword {
    if (self.curentGesturePassword.length == 0) {
        if (gesturesPassword.length <4) {
            self.statusLabel.text = @"至少连接四个点，请重新输入";
            [self shakeAnimationForView:self.statusLabel];
            return;
        }
        if (self.resetGesturesPasswordButton.hidden == YES) {
            self.resetGesturesPasswordButton.hidden = NO;
        }
        self.curentGesturePassword = gesturesPassword;
        [self.unlockPreviewView setGesturesPassword:gesturesPassword];
        self.statusLabel.text = @"再次确认解锁图案";
        return;
    }
    if ([self.curentGesturePassword isEqualToString:gesturesPassword]) {//绘制成功
        //保存手势密码
        
        [YWUnlockView saveGesturesPassword:gesturesPassword];
        BaseTabBarViewController * mainTab = [[BaseTabBarViewController alloc] initWithNibName:nil bundle:nil];
        [UIApplication sharedApplication].keyWindow.rootViewController = mainTab;
        !self.block?:self.block(YES);
        [self hide];
    }else {
        self.statusLabel.text = @"与上一次绘制不一致，请重新绘制";
        [self shakeAnimationForView:self.statusLabel];
    }
}

//验证手势密码
- (void)validateGesturesPassword:(NSString *)gesturesPassword {
    static NSInteger errorCount = 5;
    if ([gesturesPassword isEqualToString:[YWUnlockView getGesturesPassword]]) {
        errorCount = 5;
        [JQLiveTools cleanEnterBackgroundDate];
        !self.block?:self.block(YES);
        [self hide];
    }else {
        if (errorCount - 1 == 0) {//你已经输错五次了！ 退出登陆！
            
            MYSystemAlertView *v = [[MYSystemAlertView alloc] init];

            v.my_title = @"手势密码已失效";
            v.my_content = @"请重新登陆";
            v.my_rightButtonTitle = @"重新登陆";
       v.my_rightButtonTextColor = MY_ColourMatchingSpecifications_Theme1;

            [v showOnlyOneButtonWithCompletion:^(MYSystemAlertView * _Nonnull vc) {
                [JQLiveTools deleteGesturePassword];
                [JQLiveTools logout];
            }];
            
            
            return;
        }
        self.statusLabel.text = [NSString stringWithFormat:@"密码错误，还可以再输入%ld次",--errorCount];
        [self shakeAnimationForView:self.statusLabel];
    }
}

//抖动动画
- (void)shakeAnimationForView:(UIView *)view{
    CALayer *viewLayer = view.layer;
    CGPoint position = viewLayer.position;
    CGPoint left = CGPointMake(position.x - 10, position.y);
    CGPoint right = CGPointMake(position.x + 10, position.y);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [animation setFromValue:[NSValue valueWithCGPoint:left]];
    [animation setToValue:[NSValue valueWithCGPoint:right]];
    [animation setAutoreverses:YES]; // 平滑结束
    [animation setDuration:0.08];
    [animation setRepeatCount:3];
    [viewLayer addAnimation:animation forKey:nil];
}

#pragma mark - public methods
/** 展示 手势密码视图 */
+ (void)showUnlockViewWithType:(YWUnlockViewType)type callBack:(CallBackBlock)callBack{
    if(type == YWUnlockViewUnlock && ![YWUnlockView getGesturesPassword].length) return;
    YWUnlockView *unlockView = [YWUnlock_BD loadNibNamed:@"YWUnlockView" owner:nil options:nil].lastObject;
    unlockView.frame = CGRectMake(0 , YWUnlock_SH, YWUnlock_SW, YWUnlock_SH);
    [YWUnlock_KW addSubview:unlockView];
    unlockView.block = [callBack copy];
    unlockView.type = type;
    
    if (type == YWUnlockViewCreate) {
        QMUIButton *navigationButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
        navigationButton.imagePosition = QMUIButtonImagePositionRight;
        [navigationButton setImage:UIImageMake(@"ic_shop_gray_arrow") forState:UIControlStateNormal];
        [navigationButton setTitle:@"跳过 " forState:UIControlStateNormal];
        [navigationButton setTitleColor:MY_Text_Color_Theme2 forState:0];
        navigationButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [navigationButton addTarget:self action:@selector(skipViewTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [unlockView addSubview:navigationButton];
        
        [navigationButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@40);
            make.right.equalTo(unlockView).offset(-MY_SpacingAndMargin_Spa5);
            make.top.equalTo(@(MY_SpacingAndMargin_Spa5));
        }];
    }
    [UIView animateWithDuration:0.25 animations:^{
        unlockView.frame = CGRectMake(0, MY_StatusBarHeight - 20, YWUnlock_SW, YWUnlock_SH);
    }];
}
/** 展示 验证手势密码视图 */
- (void)showConfigView
{
    if(![YWUnlockView getGesturesPassword].length) return;
    self.frame = CGRectMake(0 , YWUnlock_SH, YWUnlock_SW, YWUnlock_SH);
    [YWUnlock_KW addSubview:self];
    self.type = YWUnlockViewUnlock;
    self.isShow = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(0, MY_StatusBarHeight - 20, YWUnlock_SW, YWUnlock_SH);
    }];
}

/** 隐藏视图 */
- (void)hide{
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(0, YWUnlock_SH, YWUnlock_SW, YWUnlock_SH);
    } completion:^(BOOL finished) {
        self.isShow = NO;
        [self removeFromSuperview];
    }];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    !self.block?:self.block(NO);
    [self hide];
}


#pragma mark - event response
//返回按钮点击
- (IBAction)backButtonClick:(UIButton *)sender {
    !self.block?:self.block(NO);
    [self hide];
}

//点击重新绘制按钮
- (IBAction)resetGesturePassword:(id)sender {
    self.curentGesturePassword = nil;
    self.statusLabel.text = @"绘制解锁图案";
    self.resetGesturesPasswordButton.hidden = YES;
    [self.unlockPreviewView setGesturesPassword:@""];
}

#pragma mark - getters and setters
/** 设置密码的操作类型 */
- (void)setType:(YWUnlockViewType)type{
    _type = type;
    self.unlockPreviewView.hidden = _type != YWUnlockViewCreate;
    self.iconImageView.hidden = _type == YWUnlockViewCreate;
    YWUnlock_WKSELF;
    [self.gesturesUnlockView setDrawRectFinishedBlock:^(NSString *gesturePassword) {
        DLog(@"gesturePassword=%@",gesturePassword);
        [weakSelf handleWithType:type password:gesturePassword];
    }];
}

#pragma mark - other methods
/** 是否已经创建过手势密码 */
+ (BOOL)haveGesturePassword{
    return [YWUnlockView getGesturesPassword].length?YES:NO;
}

/** 删除手势密码 */
+ (void)deleteGesturesPassword{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:MYGesturesPassword];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/** 保存手势密码 */
+ (void)saveGesturesPassword:(NSString *)gesturesPassword {
    [[NSUserDefaults standardUserDefaults] setObject:gesturesPassword forKey:MYGesturesPassword];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/** 获取手势密码 */
+ (NSString *)getGesturesPassword{
    return [[NSUserDefaults standardUserDefaults] objectForKey:MYGesturesPassword];
}


//跳过
- (void)skipViewTouchUpInside
{
    BaseTabBarViewController *mainTab = [[BaseTabBarViewController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = mainTab;
}


@end
